package com.github.rnewson.couchdb.lucene;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

//使用java自带线程池的生成器池
public class GenePollUseThreadPoll implements AbstractGenerationPoll{
	GeneUseThreadPoll[] generationPoll = null;
	int sizepoll = 20;
	int currentsizepoll = 3;
	int currentposition = 0;
	int maxsizepoll = 5;
	//String databasename = null;
	ThreadPoolExecutor threadpoll = null;
	static GenePollUseThreadPoll globalGenerationPoll = null;
	
	public GenePollUseThreadPoll(int sizepoll)
	{
		this.sizepoll = sizepoll;
	}
	
	public AbstractGenerationPoll getGenerationPoll()
	{
		return initGenerationPoll(this.sizepoll);
	}
//	static public GenerationPoll initGenerationPoll(String databasename)
//	{
//		if(globalGenerationPoll == null)
//		{
//			globalGenerationPoll = new GenerationPoll();
//			globalGenerationPoll.initializePoll(databasename);
//			return globalGenerationPoll;
//		}
//		else
//		{
//			return globalGenerationPoll;
//		}
//	}
	
	static private AbstractGenerationPoll initGenerationPoll(int sizepoll)
	{
		if(globalGenerationPoll == null)
		{
			globalGenerationPoll = new GenePollUseThreadPoll(sizepoll);
			globalGenerationPoll.initializePoll(sizepoll);
			return globalGenerationPoll;
		}
		else
		{
			return globalGenerationPoll;
		}
	}
	
	public void setSizePoll(int sizepoll)
	{
		this.sizepoll = sizepoll;
	}
	public int getSizePoll()
	{
		return this.sizepoll;
	}
//	
//	private void initializePoll(String databasename)
//	{
//		initializePoll(maxsizepoll);
//	}
//	
	protected void initializePoll(int sizepoll)//初始化网页生成线程池，最新三个线程，并申请三个线程
	{
		threadpoll = new ThreadPoolExecutor(2, 30, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(30),
				new ThreadPoolExecutor.DiscardOldestPolicy());
		//this.databasename = databasename;
		if(sizepoll < 3)
			sizepoll = 3;
		generationPoll = new GeneUseThreadPoll[sizepoll];
//		for(int i = 0; i < 3; i++)
//		{
//			generationPoll[i] = new GeneUseThreadPoll();
//			generationPoll[i].initGeneration(databasename);
//			//Thread thread = new Thread(generationPoll[i]);
//			
//			//generationPoll[i].setThread(thread);//设置generationPoll[currentsizepoll]所属的线程
//			
//			//threadpoll.execute(generationPoll[i]);
//			//thread.start();
//		}
		this.currentposition = 0;
		this.sizepoll = sizepoll;
		this.currentsizepoll = 0;
	}
	
	public AbstractGeneration getGeneration(String databasename)
	{
		int travelNum = 0;
		//this.currentposition++;
		
		if(this.currentsizepoll == 0)
		{
			generationPoll[0] = new GeneUseThreadPoll();
			generationPoll[0].initGeneration(databasename);
			this.currentposition = 0;
			this.currentsizepoll = 1;
		}
		this.currentposition = this.currentposition % this.currentsizepoll;
		while(generationPoll[this.currentposition].getStoped() == false)
		{
			travelNum++;
			this.currentposition++;
			this.currentposition = this.currentposition % this.currentsizepoll;

			if(this.currentsizepoll < this.sizepoll &&
					travelNum >= this.currentsizepoll)
			{
				generationPoll[this.currentsizepoll] = new GeneUseThreadPoll();
				generationPoll[this.currentsizepoll].initGeneration(databasename);
				//Thread thread = new Thread(generationPoll[currentsizepoll]);
				
				//generationPoll[currentsizepoll].setThread(thread);//设置generationPoll[currentsizepoll]所属的线程
				
				//threadpoll.execute(generationPoll[this.currentsizepoll]);
				
				//thread.start();
				this.currentposition = this.currentsizepoll;
				this.currentsizepoll++;
			}
		}
		
		int position = this.currentposition;
		this.currentposition++;
		
		generationPoll[position].setStoped(false);//把线程停止标志设为false
		generationPoll[position].setDatabasename(databasename);
		threadpoll.execute(generationPoll[position]);//在一个线程中启动generationPoll[position]
		
		return generationPoll[position];
	}
	
	public void closeThread()//关闭网页生成线程，只留下3个
	{
//		for(int i = 3; i < generationPoll.length; i++)
//		{
//			generationPoll[i].stopThread(true);
//			generationPoll[i] = null;
//		}
	}
}
